package cn.hs.controller;

import cn.hs.entity.Systemuser;
import cn.hs.entity.Systemuser;
import cn.hs.service.SystemuserService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName SystemuserController
 * @Description TODO
 * @Author CnYuu
 * @Date 2024年10月22日 16:58
 * @Version 1.0
 */
@RestController    // 返回值为json格式字符串
@RequestMapping("/systemuser")
public class SystemuserController {
    @Autowired
    private SystemuserService systemuserService;

    /**
     * @RequestBody  : 限制全端请求参数为json格式字符串
     * @author libowen
     * @date 2024/3/29 9:47
     * @param us
     * @return java.lang.Object
     */
    @PostMapping("/login")
    public Object login(@RequestBody Systemuser us){
        Systemuser user = systemuserService.login(us.getUsername(), us.getPassword());
        // 用户不存在
        if(user==null){
            return Result.error("用户名不存在！");
        }
        // 密码错误
        if(user.getUsername()==null){
            return Result.error("密码错误！");
        }
        // 登录成功
        return Result.success(user);
    }


    @PostMapping("/insert")
    public Result insert(@RequestBody Systemuser systemuser){
        int add = systemuserService.insert(systemuser);
        if (add == 1) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }
    @PostMapping("/update")
    public Result update(@RequestBody Systemuser systemuser){
        int edit = systemuserService.update(systemuser);
        if(edit==1){
            return  Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
    @GetMapping("/find")
    public Result find(@RequestBody(required = false) Systemuser systemuser){
        List<Systemuser> list=systemuserService.find(systemuser);
        Result result=Result.success(list);
        return result;
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
//        System.out.println("id = " + id);
//        long l = Long.parseLong(id);
        int remove = systemuserService.delete(id);
        if(remove==1){
            return  Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(value = "pageNum", required = false,
                    defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false,
                    defaultValue = "5") Integer pageSize,
            Systemuser systemuser
    ) {
        PageInfo<Systemuser> pageInfo = systemuserService.findAll(pageNum, pageSize,systemuser);
        return Result.success(pageInfo);
    }
}
